<template>
  <div class="home-container">
    <div class="welcome-section">
      <h1>欢迎使用化工智能安全监护管理系统</h1>
      <p>数据驱动安全，智能守护生命</p>
    </div>
    
    <!-- 统计卡片 -->
    <div class="stats-grid">
      <el-card class="stat-card" shadow="hover">
        <div class="stat-content">
          <div class="stat-icon user-icon">
            <el-icon size="30"><User /></el-icon>
          </div>
          <div class="stat-info">
            <h3>{{ userStats.total }}</h3>
            <p>总用户数</p>
          </div>
        </div>
      </el-card>
      
      <el-card class="stat-card" shadow="hover">
        <div class="stat-content">
          <div class="stat-icon role-icon">
            <el-icon size="30"><UserFilled /></el-icon>
          </div>
          <div class="stat-info">
            <h3>{{ roleStats.total }}</h3>
            <p>总角色数</p>
          </div>
        </div>
      </el-card>
      
      <el-card class="stat-card" shadow="hover">
        <div class="stat-content">
          <div class="stat-icon active-icon">
            <el-icon size="30"><CircleCheck /></el-icon>
          </div>
          <div class="stat-info">
            <h3>{{ userStats.active }}</h3>
            <p>活跃用户</p>
          </div>
        </div>
      </el-card>
      
      <el-card class="stat-card" shadow="hover">
        <div class="stat-content">
          <div class="stat-icon today-icon">
            <el-icon size="30"><Calendar /></el-icon>
          </div>
          <div class="stat-info">
            <h3>{{ userStats.today }}</h3>
            <p>今日新增</p>
          </div>
        </div>
      </el-card>
    </div>
    
    <!-- 快速操作 -->
    <div class="quick-actions">
      <h2>快速操作</h2>
      <div class="actions-grid">
        <el-card class="action-card" shadow="hover" @click="goToUsers">
          <div class="action-content">
            <el-icon size="40" color="#409EFF"><User /></el-icon>
            <h3>用户管理</h3>
            <p>管理系统用户，创建、编辑、删除用户信息</p>
          </div>
        </el-card>
        
        <el-card class="action-card" shadow="hover" @click="goToRoles">
          <div class="action-content">
            <el-icon size="40" color="#67C23A"><UserFilled /></el-icon>
            <h3>角色管理</h3>
            <p>管理用户角色，设置权限和访问控制</p>
          </div>
        </el-card>
        
        <el-card class="action-card" shadow="hover" @click="createUser">
          <div class="action-content">
            <el-icon size="40" color="#E6A23C"><Plus /></el-icon>
            <h3>新建用户</h3>
            <p>快速创建新用户账户</p>
          </div>
        </el-card>
        
        <el-card class="action-card" shadow="hover" @click="createRole">
          <div class="action-content">
            <el-icon size="40" color="#F56C6C"><Setting /></el-icon>
            <h3>新建角色</h3>
            <p>创建新的用户角色</p>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import {
  User,
  UserFilled,
  CircleCheck,
  Calendar,
  Plus,
  Setting
} from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

// 统计数据
const userStats = ref({
  total: 0,
  active: 0,
  today: 0
})

const roleStats = ref({
  total: 0
})

// 导航到用户管理
const goToUsers = () => {
  router.push('/dashboard/users')
}

// 导航到角色管理
const goToRoles = () => {
  router.push('/dashboard/roles')
}

// 创建用户
const createUser = () => {
  router.push('/dashboard/users?action=create')
}

// 创建角色
const createRole = () => {
  router.push('/dashboard/roles?action=create')
}

// 加载统计数据
const loadStats = async () => {
  try {
    // 获取用户和角色数据来计算统计
    await userStore.fetchUsers()
    await userStore.fetchRoles()
    
    userStats.value.total = userStore.users.length
    userStats.value.active = userStore.users.filter(user => user.status === 'active').length
    userStats.value.today = userStore.users.filter(user => {
      const today = new Date().toDateString()
      const userDate = new Date(user.createdAt || user.createTime).toDateString()
      return today === userDate
    }).length
    
    roleStats.value.total = userStore.roles.length
  } catch (error) {
    console.error('加载统计数据失败:', error)
    // 设置默认值
    userStats.value = { total: 0, active: 0, today: 0 }
    roleStats.value = { total: 0 }
  }
}

onMounted(() => {
  loadStats()
})
</script>

<style scoped>
.home-container {
  max-width: 1200px;
  margin: 0 auto;
}

.welcome-section {
  text-align: center;
  margin-bottom: 40px;
  padding: 40px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 10px;
}

.welcome-section h1 {
  font-size: 32px;
  margin-bottom: 10px;
  font-weight: 600;
}

.welcome-section p {
  font-size: 16px;
  opacity: 0.9;
  margin: 0;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  margin-bottom: 40px;
}

.stat-card {
  border-radius: 10px;
  transition: transform 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-5px);
}

.stat-content {
  display: flex;
  align-items: center;
  padding: 10px;
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 20px;
}

.user-icon {
  background-color: #e3f2fd;
  color: #1976d2;
}

.role-icon {
  background-color: #e8f5e8;
  color: #388e3c;
}

.active-icon {
  background-color: #fff3e0;
  color: #f57c00;
}

.today-icon {
  background-color: #fce4ec;
  color: #c2185b;
}

.stat-info h3 {
  font-size: 28px;
  font-weight: 600;
  margin: 0 0 5px 0;
  color: #333;
}

.stat-info p {
  font-size: 14px;
  color: #666;
  margin: 0;
}

.quick-actions h2 {
  margin-bottom: 20px;
  color: #333;
  font-size: 24px;
  font-weight: 600;
}

.actions-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 20px;
}

.action-card {
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 2px solid transparent;
}

.action-card:hover {
  transform: translateY(-5px);
  border-color: #409EFF;
  box-shadow: 0 8px 25px rgba(64, 158, 255, 0.2);
}

.action-content {
  text-align: center;
  padding: 20px;
}

.action-content h3 {
  margin: 15px 0 10px 0;
  color: #333;
  font-size: 18px;
  font-weight: 600;
}

.action-content p {
  color: #666;
  font-size: 14px;
  line-height: 1.5;
  margin: 0;
}
</style>